#pragma once 

#include <iostream>
#include "sock.hpp"
#include "log.hpp"
#include <algorithm>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <poll.h>

const uint16_t default_port = 8080;
const int Fd_Max_Num = 1024;
const int default_fd = -1;
const int no_envent = 0;

class PolltServer
{
public:
    PolltServer(uint16_t port = default_port)
        : _port(port)
    {
        for(int i = 0; i < Fd_Max_Num; i++)
        {
            _envent_fds[i].fd = default_fd;
            _envent_fds[i].events = no_envent;
            _envent_fds[i].revents = no_envent;
        }
    }

    ~PolltServer()
    {
        _listen_sock.Close();
    }

    void PollInit()
    {
        _listen_sock.Socket();
        _listen_sock.Bind(_port);
        _listen_sock.Listen();
    }

    void Print()
    {
        for(int i = 0; i < Fd_Max_Num; i++)
        {
            if(_envent_fds[i].fd == default_fd)
                continue;
            std::cout << _envent_fds[i].fd << " ";
        }
        std::cout << std::endl;
    }

    void Accept()
    {
        std::string client_ip;
        uint16_t client_port;
        int fd = _listen_sock.Accept(client_ip, client_port);
        if(fd < 0)
        {
            std::cout << "accept error" << std::endl;
            return;
        }
        
        int pos = 0;
        for(; pos < Fd_Max_Num; pos++)
        {
            if(_envent_fds[pos].fd != default_fd)
                continue;
            else
                break;
        }
        if(pos == Fd_Max_Num)
        {
            std::cout << "扩容" << std::endl;
            return;
        }

        _envent_fds[pos].fd = fd;
        _envent_fds[pos].events = POLLIN;
        Print();

    }

    void Read(int fd, int pos)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
        if(n < 0)
        {
            std::cout << "读取失败" << std::endl;
            return;
        }
        else if(n > 0)
        {
            buffer[n] = '\0';
            std::cout << "message : " << buffer << std::endl;
        }
        else
        {
            std::cout << "文件关闭" << std::endl;
            _envent_fds[pos].fd = default_fd;
            close(fd);
        }
    }

    void handler()
    {
        // 遍历 看哪一个文件描述符好了
        for(int i = 0; i < Fd_Max_Num; i++)
        {
            int fd = _envent_fds[i].fd;
            if(fd == default_fd)
                continue;

            if(_envent_fds[i].revents & POLLIN)
            {
                if(fd == _listen_sock.GetSockfd())
                {
                    // 处理新链接
                    Accept();
                }
                else
                {
                    // 读取数据
                    Read(fd, i);
                }
            }
        }
    }


    void Run()
    {
        _envent_fds[0].fd = _listen_sock.GetSockfd();
        _envent_fds[0].events = POLLIN;
        _envent_fds[0].revents = no_envent;
        while(1)
        {

            // timeout 等待时间 -1 为阻塞
            int n = poll(_envent_fds, Fd_Max_Num, /*int timeout*/ 1000);
            if(n > 0)
            {
                // 处理
                std::cout << "资源就绪， 处理ing" << std::endl;
                handler();
            }
            else if(n < 0)
            {
                std::cout << "select error " << std::endl;
            }
            else if(n == 0)
            {
                // std::cout << "timeout : " << timeout.tv_sec << " " << timeout.tv_usec << std::endl;
                std::cout << "n == 0, 资源没有就绪" << std::endl; 
            }
        }
    }
private:
    uint32_t _port = 8080;
    Sock _listen_sock;
    struct pollfd _envent_fds[Fd_Max_Num];

};